<template>
  <div class="profile-container">
    <el-card class="profile-card">
      <template #header>
        <div class="card-header">
          <span>个人信息</span>
          <el-button type="text" @click="handleEdit">编辑</el-button>
        </div>
      </template>
      <div class="profile-content">
        <div class="avatar-container">
          <el-avatar :size="100" :src="user.avatar" />
          <el-upload
            v-if="isEditing"
            class="avatar-uploader"
            action="/api/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <el-button size="small">更换头像</el-button>
          </el-upload>
        </div>
        <div class="info-container">
          <el-form
            ref="formRef"
            :model="form"
            :rules="rules"
            label-width="80px"
            :disabled="!isEditing"
          >
            <el-form-item label="用户名" prop="username">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" />
            </el-form-item>
            <el-form-item label="性别" prop="gender">
              <el-select v-model="form.gender" style="width: 100%">
                <el-option label="男" value="男" />
                <el-option label="女" value="女" />
              </el-select>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" />
            </el-form-item>
            <el-form-item label="积分">
              <el-input v-model="form.points" disabled />
            </el-form-item>
            <el-form-item v-if="isEditing">
              <el-button type="primary" @click="handleSave">保存</el-button>
              <el-button @click="handleCancel">取消</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { updateUser } from '../../api'

export default {
  name: 'Profile',
  setup() {
    const user = ref(JSON.parse(localStorage.getItem('user') || '{}'))
    const isEditing = ref(false)
    const formRef = ref(null)

    const form = reactive({
      username: '',
      name: '',
      gender: '',
      phone: '',
      points: 0
    })

    const rules = {
      username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
      name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
      gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
      phone: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
      ]
    }

    const initForm = () => {
      Object.assign(form, user.value)
    }

    const handleEdit = () => {
      isEditing.value = true
    }

    const handleSave = async () => {
      try {
        await formRef.value.validate()
        await updateUser(user.value.id, form)
        Object.assign(user.value, form)
        localStorage.setItem('user', JSON.stringify(user.value))
        isEditing.value = false
        ElMessage.success('保存成功')
      } catch (error) {
        ElMessage.error('保存失败')
      }
    }

    const handleCancel = () => {
      isEditing.value = false
      initForm()
    }

    const handleAvatarSuccess = (response) => {
      user.value.avatar = response.url
      localStorage.setItem('user', JSON.stringify(user.value))
      ElMessage.success('头像更新成功')
    }

    onMounted(() => {
      initForm()
    })

    return {
      user,
      form,
      rules,
      isEditing,
      formRef,
      handleEdit,
      handleSave,
      handleCancel,
      handleAvatarSuccess
    }
  }
}
</script>

<style scoped>
.profile-container {
  padding: 20px;
}

.profile-card {
  max-width: 800px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.profile-content {
  display: flex;
  gap: 40px;
  padding: 20px;
}

.avatar-container {
  text-align: center;
}

.avatar-uploader {
  margin-top: 20px;
}

.info-container {
  flex: 1;
}
</style> 